<template>
  <div class="kw_wrap">
    <van-empty class="empty" image="https://fastly.jsdelivr.net/npm/@vant/assets/custom-empty-image.png" image-size="65"
      description="正在努力搜索中..." v-if="!queryResultList.length" />
    <!-- 有声书 -->
    <div v-else>
      <div class="kw_song" v-for="item in albumResultList" :key="item.id">
        <div class="kw_img">
          <img class="img" :src="item.imgPath" />

          <div>
            <span class="span" v-html="item.highlightKeyword"></span>

            <div class="category">{{ item.category }}</div>
            <van-icon name="service-o" class="icon" />
            <span class="span1">19.14亿</span>
            <van-icon name="bullhorn-o" class="icon" />
            <span class="span1">{{ item.tracks }}</span>
          </div>
        </div>

        <van-icon name="star" class="star" />
      </div>
      <van-divider />
      <!-- 热门 -->

      <div class="kw_item" v-for="kw in queryResultList" :key="kw.id" @click="toSearchHandler(kw.highlightKeyword, kw)"
        v-html="kw.highlightKeyword"></div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import hotApi, { type AlbumModel, type QueryModel } from '@/api/serach'
import { useRouter } from 'vue-router'

const router = useRouter()

// 搜索框数据 --有声书
const albumResultList = ref<AlbumModel[]>([])
const albumResultNum = ref(0)
// 搜索历史数据
let history1 = ref([])

// hotDay组件传过来的input value值
const props = defineProps<{
  item: any
}>()

// 搜索框数据 -- 关键字
const queryResultList = ref<QueryModel[]>([])
const queryResultNum = ref(0)

// 点击搜索实时列表将数据存入本地
const toSearchHandler = (item: any, kw: any) => {
  if (!!localStorage.getItem('kwHistory')) {
    if (JSON.parse(localStorage.getItem('kwHistory')).includes(kw.highlightKeyword)) {
      router.push({
        name: 'Search',
        query: {
          kw: item
        }
      })
    } else {
      history1.value = [...JSON.parse(localStorage.getItem('kwHistory')), kw.highlightKeyword]
      localStorage.setItem('kwHistory', JSON.stringify(history1.value))
    }
  } else {
    history1.value.push(kw.highlightKeyword)
    localStorage.setItem('kwHistory', JSON.stringify(history1.value))
  }

  router.push({
    name: 'Search',
    query: {
      kw: item
    }
  })
}

// 搜索框关键词接口数据
const getSearchList = async () => {
  try {
    let result = await hotApi.reqSearch(props.item, 'true')
    albumResultList.value = result.data.data.result.albumResultList
    albumResultList.value = result.data.data.result.albumResultList
    queryResultList.value = result.data.data.result.queryResultList
    albumResultNum.value = result.data.data.result.albumResultNum
    queryResultNum.value = result.data.data.result.queryResultNum
  } catch (error) {
    console.log(error)
  }
}

// 监听input的value值变化
const timer = ref(null)
watch(
  () => props.item,
  () => {
    if (timer.value) return
    timer.value = setTimeout(async () => {
      await getSearchList()
      timer.value = null
    }, 500)
  },
  {
    deep: true,
    immediate: true
  }
)
</script>

<style scoped lang="scss">
.kw_wrap {
  .kw_song {
    width: 342px;
    font-size: 14px;
    color: #40404c;
    position: relative;

    .star {
      position: absolute;
      right: -16px;
      top: 2px;
      font-size: 29px;
      color: #a3a3ac;
    }

    .kw_img {
      display: flex;

      .img {
        width: 40px;
        height: 40px;
        margin-left: 15px;
        border-radius: 5px;
        padding-right: 10px;
      }

      .btn {
        height: 16px;
        width: 60px;
        line-height: 18px;
        color: #a3a3ac;
        margin-right: 10px;
      }

      .span {
        display: block;
        width: 270px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        -o-text-overflow: ellipsis;
        margin-bottom: 8px;
      }

      .icon {
        color: #a3a3ac;
      }

      .span1 {
        color: #a3a3ac;
        margin-right: 10px;
      }
    }
  }
}

.kw_item {
  width: 342px;
  height: 40px;
  line-height: 40px;
  font-family: PingFangSC-Regular;
  font-size: 14px;
  color: #2e2e35;
  border-bottom: rgba(163, 163, 172, 0.2) solid 1px;
  margin-left: 20px;
}

.category {
  display: inline-block;
  height: 16px;
  width: 60px;
  line-height: 16px;
  text-align: center;
  color: #a3a3ac;
  margin-right: 10px;
  border-radius: 5px;
  border: rgba(163, 163, 172, 0.2) solid 1px;
}
</style>
